package su.api.mock.config;

import java.util.Optional;
import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import su.api.mock.utils.CommonCacheOperator;

@Component
public class RedisCacheResolver implements SwitchResolver {

    @Resource
    private CommonCacheOperator commonCacheOperator;

    @Override
    public boolean resolver(String key) {
        try {
            return Optional.ofNullable(commonCacheOperator.get(key)).map(String::valueOf).map(Integer::valueOf)
                    .orElse(unExpectValue) == on;
        }catch (Exception e) {
            return false;
        }
    }

    @Override
    public void config(String key, Integer onoff) {
        commonCacheOperator.put(key, onoff);
    }
}
